package app.itgungnir.compose.wan.business.main.home

import android.content.res.Configuration
import androidx.annotation.DrawableRes
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.LocalFireDepartment
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.text.HtmlCompat
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import app.itgungnir.compose.wan.R
import app.itgungnir.compose.wan.Routes
import app.itgungnir.compose.wan.theme.WanComposePreviewTheme
import app.itgungnir.compose.wan.utils.LocalDataStore
import app.itgungnir.compose.wan.widgets.*
import com.google.accompanist.flowlayout.FlowRow

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HomeToolBox(navController: NavController = rememberNavController()) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(start = 15.dp, end = 15.dp, top = 10.dp)
    ) {
        HomeToolItem(
            modifier = Modifier.weight(1F),
            drawableRes = R.drawable.svg_home_tool_project,
            title = "项目"
        ) { }
        Spacer(modifier = Modifier.width(10.dp))
        HomeToolItem(
            modifier = Modifier.weight(1F),
            drawableRes = R.drawable.svg_home_tool_wx,
            title = "公众号"
        ) { }
        Spacer(modifier = Modifier.width(10.dp))
        HomeToolItem(
            modifier = Modifier.weight(1F),
            drawableRes = R.drawable.svg_home_tool_knowledge,
            title = "知识体系"
        ) { }
        Spacer(modifier = Modifier.width(10.dp))
        HomeToolItem(
            modifier = Modifier.weight(1F),
            drawableRes = R.drawable.svg_home_tool_question,
            title = "问答"
        ) { }
    }
}

@Composable
fun HomeArticleItem(
    navController: NavController = rememberNavController(),
    item: HomeArticle?,
    hot: Boolean = false,
    collectCallback: (Boolean) -> Unit
) {
    item ?: return
    WanCardRow(onClick = { /* TODO */ }) {
        item.envelopePic?.takeIf { it.isNotEmpty() }?.let { url ->
            WanNetworkImage(modifier = Modifier.padding(end = 10.dp), width = 100.dp, height = 150.dp, url = url)
        }
        Column {
            Row(verticalAlignment = Alignment.CenterVertically) {
                if (hot) {
                    Icon(
                        modifier = Modifier.padding(end = 5.dp),
                        imageVector = Icons.Outlined.LocalFireDepartment,
                        tint = MaterialTheme.colors.primary,
                        contentDescription = ""
                    )
                }
                WanTitle(content = HtmlCompat.fromHtml("${item.title}", HtmlCompat.FROM_HTML_MODE_COMPACT).toString())
            }
            FlowRow {
                item.tags?.takeIf { it.isNotEmpty() }?.forEach { tag ->
                    WanTagButton(
                        modifier = Modifier.padding(top = 2.dp, end = 5.dp),
                        text = tag.name,
                        color = MaterialTheme.colors.secondary
                    ) {}
                }
            }
            Spacer(modifier = Modifier.height(3.dp))
            item.desc?.takeIf { it.isNotEmpty() }?.let {
                WanDescription(
                    modifier = Modifier.padding(bottom = 3.dp),
                    content = HtmlCompat.fromHtml("${item.desc}", HtmlCompat.FROM_HTML_MODE_COMPACT).toString()
                )
            }
            Row(verticalAlignment = Alignment.Bottom) {
                Column(modifier = Modifier.weight(1F)) {
                    Row(modifier = Modifier.padding(bottom = 3.dp), verticalAlignment = Alignment.CenterVertically) {
                        WanTagButton(text = item.superChapterName) {}
                        WanInfo(content = " / ")
                        WanTagButton(text = item.chapterName) {}
                    }
                    WanInfo(content = item.author, prefix = "作者：")
                    WanInfo(content = item.shareUser, prefix = "分享人：")
                    WanInfo(content = item.niceDate?.ifEmpty { item.niceShareDate } ?: item.niceShareDate)
                }
                WanCollectButton(item = item, onClick = {
                    when (LocalDataStore.localData.hasLogin) {
                        true -> collectCallback.invoke(it)
                        else -> navController.navigate(Routes.LOGIN)
                    }
                })
            }
        }
    }
}

@Composable
private fun HomeToolItem(
    modifier: Modifier = Modifier,
    @DrawableRes drawableRes: Int,
    title: String,
    onClick: () -> Unit
) {
    Column(
        modifier = modifier
            .clip(shape = RoundedCornerShape(size = 12.dp))
            .background(color = MaterialTheme.colors.background)
            .clickable { onClick.invoke() }
            .padding(start = 10.dp, end = 10.dp, top = 5.dp, bottom = 10.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(modifier = Modifier.size(50.dp), painter = painterResource(id = drawableRes), contentDescription = "")
        WanDescription(content = title, maxLines = 1)
    }
}

@Preview(name = "HomeToolBox light mode", showBackground = false)
@Preview(name = "HomeToolBox dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = false)
@Composable
private fun HomeToolBoxPreview() {
    WanComposePreviewTheme {
        HomeToolBox()
    }
}

@Preview(name = "HomeArticleItem light mode", showBackground = false)
@Preview(name = "HomeArticleItem dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = false)
@Composable
private fun HomeArticleItemPreview() {
    WanComposePreviewTheme {
        val article = HomeArticle(
            title = "标题标题标题标题标题标题标题标题标题标题",
            desc = "简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介",
            author = "作者作者作者",
            shareUser = "分享人分享人分享人",
            chapterName = "我的小号",
            superChapterName = "公众号",
            collect = false,
            niceDate = "3天前",
            niceShareDate = "2天前",
            envelopePic = "https://www.wanandroid.com/resources/image/pc/default_project_img.jpg",
            tags = listOf(
                HomeArticle.HomeArticleTag(name = "问答"),
                HomeArticle.HomeArticleTag(name = "Android"),
                HomeArticle.HomeArticleTag(name = "Kotlin"),
                HomeArticle.HomeArticleTag(name = "A nice tag"),
                HomeArticle.HomeArticleTag(name = "Jetpack Compose")
            )
        )
        HomeArticleItem(item = article) {}
    }
}
